package com.advert.boot.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.advert.boot.system.service.AdvertFileService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.advert.boot.core.security.util.SecurityUtils;
import com.advert.boot.system.mapper.AdvertProductMapper;
import com.advert.boot.system.model.entity.AdvertProduct;
import com.advert.boot.system.model.form.ProductForm;
import com.advert.boot.system.model.query.ProductPageQuery;
import com.advert.boot.system.model.vo.OrderInfoVO;
import com.advert.boot.system.model.vo.ProductDetailsVO;
import com.advert.boot.system.model.vo.ProductVO;
import com.advert.boot.system.model.vo.StatisticsVO;
import com.advert.boot.system.service.AdvertProductService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
@RequiredArgsConstructor
public class AdvertProductServiceImpl extends ServiceImpl<AdvertProductMapper, AdvertProduct> implements AdvertProductService {
    private final AdvertFileService fileService;


    @Override
    public Page<ProductVO> page(ProductPageQuery pageQuery, int type) {
        LambdaQueryWrapper<AdvertProduct> query = new LambdaQueryWrapper<>();

        //搜索条件：默认0=全部，1=厂家/供应商名称，2=品牌名称，3=材料名称与型号，4=规格
        Integer mold = pageQuery.getType();
        if (mold != null) {
            //厂家/供应商名称
            if (mold.equals(1) && StrUtil.isNotBlank(pageQuery.getKeywords())) {
                query.like(AdvertProduct::getCompanyName, pageQuery.getKeywords());
            }
            //品牌名称
            if (mold.equals(2) && StrUtil.isNotBlank(pageQuery.getKeywords())) {
                query.like(AdvertProduct::getBrandName, pageQuery.getKeywords());
            }
            //材料名称与型号
            if (mold.equals(3) && StrUtil.isNotBlank(pageQuery.getKeywords())) {
                query.like(AdvertProduct::getMaterialNameModel, pageQuery.getKeywords());
            }
            //规格
            if (mold.equals(4) && StrUtil.isNotBlank(pageQuery.getKeywords())) {
                query.like(AdvertProduct::getSpecifications, pageQuery.getKeywords());
            }
        }
        query.orderByDesc(AdvertProduct::getId);
        IPage<AdvertProduct> page = baseMapper.selectPage(new Page<>(pageQuery.getPageNum(), pageQuery.getPageSize()), query);
        //返回结果
        Page<ProductVO> resultPage = new Page<>();
        BeanUtils.copyProperties(page, resultPage);
        List<ProductVO> resultList = new ArrayList<>();
        page.getRecords().forEach(obj -> {
            ProductVO orderVO = new ProductVO();
            BeanUtils.copyProperties(obj, orderVO);
            if (type == 2) {
                orderVO.setMaterialPackageFileId(null);
                orderVO.setMaterialDescriptionDiagramFileId(null);
                orderVO.setMaterialBackViewFileId(null);
            } else {
                orderVO.setMaterialPackageFileId(obj.getMaterialPackageFile());
                orderVO.setMaterialDescriptionDiagramFileId(obj.getMaterialDescriptionDiagramFile());
                orderVO.setMaterialBackViewFileId(obj.getMaterialBackViewFile());
            }
            resultList.add(orderVO);
        });
        resultPage.setRecords(resultList);

        return resultPage;
    }

    @Override
    public Page<OrderInfoVO> orderPage() {
        return null;
    }

    @Override
    public ProductDetailsVO selectProductById(Long id) {
        AdvertProduct advertProduct = baseMapper.selectById(id);
        ProductDetailsVO productDetailsVO = new ProductDetailsVO();
        if (Objects.nonNull(advertProduct)) {
            BeanUtils.copyProperties(advertProduct, productDetailsVO);
            productDetailsVO.setMaterialBackViewFileId(advertProduct.getMaterialBackViewFile());
            productDetailsVO.setMaterialPackageFileId(advertProduct.getMaterialPackageFile());
            productDetailsVO.setMaterialDescriptionDiagramFileId(advertProduct.getMaterialDescriptionDiagramFile());
            productDetailsVO.setCertificatesFileId(advertProduct.getCertificatesFile());
            productDetailsVO.setAntiSlipCertificationFileId(advertProduct.getAntiSlipCertificationFile());
            productDetailsVO.setFlameRetardantCertificationFileId(advertProduct.getFlameRetardantCertificationFile());
        }

        return productDetailsVO;
    }

    @Override
    public StatisticsVO statistics() {
        return null;
    }

    @Override
    public boolean save(ProductForm productForm) {
        AdvertProduct advertProduct = new AdvertProduct();
        BeanUtils.copyProperties(productForm, advertProduct);
        advertProduct.setMaterialPackageFile(productForm.getMaterialPackageFileId());
        advertProduct.setMaterialDescriptionDiagramFile(productForm.getMaterialDescriptionDiagramFileId());
        advertProduct.setMaterialBackViewFile(productForm.getMaterialBackViewFileId());
        advertProduct.setCertificatesFile(productForm.getCertificatesFileId());
        advertProduct.setAntiSlipCertificationFile(productForm.getAntiSlipCertificationFileId());
        advertProduct.setFlameRetardantCertificationFile(productForm.getFlameRetardantCertificationFileId());
        advertProduct.setCreateBy(SecurityUtils.getUserId());

        return baseMapper.insert(advertProduct) > 0;
    }

    @Override
    public boolean edit(Long id, ProductForm productForm) {
        AdvertProduct oldObj = baseMapper.selectById(id);
        Assert.isTrue(!Objects.isNull(oldObj), "该产品信息不存在");
        this.deleteFile(oldObj, productForm);

        AdvertProduct advertProduct = new AdvertProduct();
        BeanUtils.copyProperties(productForm, advertProduct);
        advertProduct.setId(id);
        advertProduct.setMaterialPackageFile(productForm.getMaterialPackageFileId());
        advertProduct.setMaterialDescriptionDiagramFile(productForm.getMaterialDescriptionDiagramFileId());
        advertProduct.setMaterialBackViewFile(productForm.getMaterialBackViewFileId());
        advertProduct.setCertificatesFile(productForm.getCertificatesFileId());
        advertProduct.setAntiSlipCertificationFile(productForm.getAntiSlipCertificationFileId());
        advertProduct.setFlameRetardantCertificationFile(productForm.getFlameRetardantCertificationFileId());
        advertProduct.setUpdateBy(SecurityUtils.getUserId());
        return baseMapper.updateById(advertProduct) > 0;
    }

    @Override
    public boolean delete(Long id) {
        if (id != null) {
            //删除附件
            AdvertProduct advertProduct = baseMapper.selectById(id);
            this.deleteFile(advertProduct);

            return baseMapper.deleteById(id) > 0;
        }
        return false;
    }

    /**
     * 删除附件信息
     */
    private void deleteFile(AdvertProduct oldObj, ProductForm product) {
        //获取产品相关附件信息
        List<Long> fileIds = new ArrayList<>();
        //材料外包装附件
        if (product.getMaterialPackageFileId() != null && oldObj.getMaterialPackageFile() != null
                && !product.getMaterialPackageFileId().equals(oldObj.getMaterialPackageFile())) {
            fileIds.add(oldObj.getMaterialPackageFile());
        }
        //材料表明图附件
        if (product.getMaterialDescriptionDiagramFileId() != null && oldObj.getMaterialDescriptionDiagramFile() != null
                && !product.getMaterialDescriptionDiagramFileId().equals(oldObj.getMaterialDescriptionDiagramFile())) {
            fileIds.add(oldObj.getMaterialDescriptionDiagramFile());
        }
        //材料背面图附件
        if (product.getMaterialBackViewFileId() != null && oldObj.getMaterialBackViewFile() != null
                && !product.getMaterialBackViewFileId().equals(oldObj.getMaterialBackViewFile())) {
            fileIds.add(oldObj.getMaterialBackViewFile());
        }
        //环保认证附件
        if (product.getCertificatesFileId() != null && oldObj.getCertificatesFile() != null
                && !product.getCertificatesFileId().equals(oldObj.getCertificatesFile())) {
            fileIds.add(oldObj.getCertificatesFile());
        }
        //防滑认证附件
        if (product.getAntiSlipCertificationFileId() != null && oldObj.getAntiSlipCertificationFile() != null
                && !product.getAntiSlipCertificationFileId().equals(oldObj.getAntiSlipCertificationFile())) {
            fileIds.add(oldObj.getAntiSlipCertificationFile());
        }
        //阻燃认证附件
        if (product.getFlameRetardantCertificationFileId() != null && oldObj.getFlameRetardantCertificationFile() != null
                && !product.getFlameRetardantCertificationFileId().equals(oldObj.getFlameRetardantCertificationFile())) {
            fileIds.add(oldObj.getFlameRetardantCertificationFile());
        }

        //删除附件
        fileService.deleteFiles(fileIds);
    }

    /**
     * 删除附件信息
     */
    private void deleteFile(AdvertProduct product) {
        //获取产品相关附件信息
        List<Long> fileIds = new ArrayList<>();
        if (Objects.nonNull(product)) {
            //材料外包装附件
            if (product.getMaterialPackageFile() != null && product.getMaterialPackageFile() > 0L) {
                fileIds.add(product.getMaterialPackageFile());
            }
            //材料表明图附件
            if (product.getMaterialDescriptionDiagramFile() != null && product.getMaterialDescriptionDiagramFile() > 0L) {
                fileIds.add(product.getMaterialDescriptionDiagramFile());
            }
            //材料背面图附件
            if (product.getMaterialBackViewFile() != null && product.getMaterialBackViewFile() > 0L) {
                fileIds.add(product.getMaterialBackViewFile());
            }
            //环保认证附件
            if (product.getCertificatesFile() != null && product.getCertificatesFile() > 0L) {
                fileIds.add(product.getCertificatesFile());
            }
            //防滑认证附件
            if (product.getAntiSlipCertificationFile() != null && product.getAntiSlipCertificationFile() > 0L) {
                fileIds.add(product.getAntiSlipCertificationFile());
            }
            //阻燃认证附件
            if (product.getFlameRetardantCertificationFile() != null && product.getFlameRetardantCertificationFile() > 0L) {
                fileIds.add(product.getFlameRetardantCertificationFile());
            }
        }

        //删除附件
        fileService.deleteFiles(fileIds);
    }
}
